//
//  STCustomerServiceViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/27.
//

import UIKit
import MessageUI

class STCustomerServiceViewController: GPBaseViewController, MFMessageComposeViewControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBarTitle("联系客服")
        addSubviewForPage()
    }
    
    func addSubviewForPage() {
        addTopBGView()
        
        let bgView: UIView = UIView.init()
        bgView.backgroundColor = UIColor.white
        bgView.layer.cornerRadius = 12
        let h = 288 * (kScreenWidth - 32) / 343
        view.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(60)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(h)
        }
        
        let img_kefu = UIImageView.init(image: UIImage.init(named: "img_kefu"))
        bgView.addSubview(img_kefu)
        img_kefu.snp.makeConstraints { (make) in
            make.top.equalTo(-40)
            make.centerX.equalTo(view.snp.centerX)
            make.width.equalTo(96)
            make.height.equalTo(96)
        }
        
        let kefu_lb = UILabel.init()
        kefu_lb.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.heavy)
        kefu_lb.textAlignment = .center
        kefu_lb.text = "在线客服"
        bgView.addSubview(kefu_lb)
        kefu_lb.snp.makeConstraints { (make) in
            make.top.equalTo(img_kefu.snp.bottom)
            make.centerX.equalTo(img_kefu.snp.centerX)
            make.height.equalTo(22)
        }
        
        let blue_View: UIView = UIView.init()
        blue_View.backgroundColor = UIColor.init(hexString: "#F4F9FF", alpha: 1)
        blue_View.layer.cornerRadius = 10
        let blue_View_h = 64 * (kScreenWidth - 32) / 311
        bgView.addSubview(blue_View)
        blue_View.snp.makeConstraints { (make) in
            make.top.equalTo(kefu_lb.snp.bottom).offset(18)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(blue_View_h)
        }
        
        let img_call = UIImageView.init(image: UIImage.init(named: "icon_lianxifangshi"))
        blue_View.addSubview(img_call)
        img_call.snp.makeConstraints { (make) in
            make.left.equalTo(12)
            make.centerY.equalTo(blue_View)
            make.width.equalTo(26)
            make.height.equalTo(26)
        }
        
        let phone_lb = UILabel.init()
        phone_lb.font = UIFont.systemFont(ofSize: 15)
        phone_lb.textAlignment = .left
        phone_lb.textColor = UIColor.init(hexString: "#323232", alpha: 1)
        phone_lb.text = "联系方式：".appending("133 3198 3835")
        bgView.addSubview(phone_lb)
        phone_lb.snp.makeConstraints { (make) in
            make.left.equalTo(img_call.snp.right).offset(8)
            make.centerY.equalTo(img_call.snp.centerY)
        }
        
        bgView.addSubview(smsBtn)
        smsBtn.snp.makeConstraints { (make) in
            make.width.equalTo(140)
            make.height.equalTo(46)
            make.left.equalTo(16)
            make.bottom.equalTo(-34)
        }
        
        bgView.addSubview(callBtn)
        callBtn.snp.makeConstraints { (make) in
            make.width.equalTo(140)
            make.height.equalTo(46)
            make.right.equalTo(-16)
            make.bottom.equalTo(-34)
        }
        
        smsBtn.addTarget(self, action: #selector(handleSendSmsAction), for: .touchUpInside)
        callBtn.addTarget(self, action: #selector(handlCallAction), for: .touchUpInside)
    }
    
    // 发短信
    @objc func handleSendSmsAction() {
        let composeVC = MFMessageComposeViewController()
        composeVC.messageComposeDelegate = self
        
        // Configure the fields of the interface.
        composeVC.recipients = ["13331983835"]
        composeVC.body = ""
        
        // Present the view controller modally.
        if MFMessageComposeViewController.canSendText() {
            self.present(composeVC, animated: true, completion: nil)
        } else {
            self.toastInfo("手机无发送短信功能")
        }
    }
    
    // 呼叫
    @objc func handlCallAction() {
        let scheme = "tel://13331983835"
        if let url = URL(string: scheme) {
            if UIApplication.shared.canOpenURL(url) {
                if #available(iOS 10, *) {
                    UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
                        print("Open \(scheme): \(success)")
                    })
                } else {
                    let success = UIApplication.shared.openURL(url)
                    print("Open \(scheme): \(success)")
                }
            } else {
                self.toastInfo("手机无拨打电话功能")
            }
        }
    }
    
    // 懒加载
    lazy var smsBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("发短信", for: .normal)
        btn.backgroundColor = UIColor.init(hexString: "#FA951E", alpha: 1)
        btn.layer.cornerRadius = 22
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        return btn
    }()
    
    lazy var callBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("呼叫", for: .normal)
        btn.backgroundColor = UIColor.init(hexString: "#3686FF", alpha: 1)
        btn.layer.cornerRadius = 22
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        return btn
    }()
    
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        controller.dismiss(animated: true, completion: nil)
    }
    
}
